package jam.UserInterface.UI;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout; //imports GridLayout library
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.File;
import java.awt.Image;

public class ButtonsGrid implements ActionListener {

	private JLabel im;
	private Image image;
	private String sname;
	private String[] sets = {"[angry]","[biggrin]","[blink]","[blush]","[bored]","[closedeyes]","[confused]","[cool]","[crying]","[cursing]",
			"[drool]","[glare]","[huh]","[laugh]","[lol]","[love]","[loveeyes]","[mad]",
			"[mellow]","[ohmy]","[rolleyes]","[sad]","[scared]","[sleep]","[smile]","[sneaky]",
			"[thumbdown]","[thumbup]","[thumbup1]","[tongue]","[tongue2]","[tongue_smilie]",
			"[unsure]","[w00t]","[wink]"};

	private JFrame frame=new JFrame(); //creates frame
	private JButton[][] grid; //names the grid of buttons
	private JEditorPane messageBox;

	public ButtonsGrid(JEditorPane messageBox){ //constructor
		this.messageBox = messageBox;
		frame.setLayout(new GridLayout(5,7)); //set layout
		grid=new JButton[5][7]; //allocate the size of grid
		int i=0;
		String currentDirectory = System.getProperty("user.dir");
		while (i<35){
			for(int y=0; y<7; y++){
				for(int x=0; x<5; x++){                       	
					String name = sets[i].concat(".gif");
					try {
					Image image = ImageIO.read(new File(currentDirectory + "/src/jam/UserInterface/Smileys/" + name));

					im = new JLabel(new ImageIcon(image));
					grid[x][y] = new JButton();
					grid[x][y].add(im);
					grid[x][y].setToolTipText(sets[i]);
					grid[x][y].addActionListener(this);			    	

					frame.add(grid[x][y]); //adds button to grid
					i++;
					}catch (Exception e){System.out.println("The file could not be read!!!!");}
				}
			}
		}

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setBounds(((int)(screenSize.getWidth()-520))/2,((int)(screenSize.getHeight()-400))/2,520,250);
		frame.setTitle("Choose a Smiley");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		//frame.pack(); //sets appropriate size for frame
		
		//change the icon code
		try {
			Image image = ImageIO.read(new File(currentDirectory + "/src/jam/UserInterface/Icons/JAMicon.png"));
			frame.setIconImage(image);
		}catch (Exception e){System.out.println("The file could not be read!!!!");}
		
		frame.setVisible(true); //makes frame visible


	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		int i = 0;
		for(int y=0; y<7; y++){
			for(int x=0; x<5; x++){   
				if (e.getSource() == grid[x][y]){
				    frame.dispose();
				    messageBox.setText(messageBox.getText() + sets[i]);
				}
				i++;
			}
		}

	}
}
